<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.7.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.7.0" type="image/png" sizes="32x32"><meta name="baidu-site-verification" content="code-aMPs2J7ZPy"><meta name="description" content="1.API                           1.1 API概述        什么是API ​	API (Application Programming Interface) ：应用程序编程接口  java中的API ​	指的就是 JDK 中提供的各种功能的 Java类，这些类将底层的实现封装了起来，我们不需要关心这些类是如何实现的，只需">
<meta property="og:type" content="article">
<meta property="og:title" content="3.常用API">
<meta property="og:url" content="http://bujiuzhi.gitee.io/2022/02/13/3.%E5%B8%B8%E7%94%A8API/index.html">
<meta property="og:site_name" content="星辰大海">
<meta property="og:description" content="1.API                           1.1 API概述        什么是API ​	API (Application Programming Interface) ：应用程序编程接口  java中的API ​	指的就是 JDK 中提供的各种功能的 Java类，这些类将底层的实现封装了起来，我们不需要关心这些类是如何实现的，只需">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s2.loli.net/2022/02/13/IVlkUfGixAwSJ5D.png">
<meta property="og:image" content="https://s2.loli.net/2022/02/13/zPuypmwNJFTSLds.png">
<meta property="article:published_time" content="2022-02-13T10:07:33.000Z">
<meta property="article:modified_time" content="2022-09-13T16:31:08.000Z">
<meta property="article:author" content="不久">
<meta property="article:tag" content="学习笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/02/13/IVlkUfGixAwSJ5D.png"><title>3.常用API | 星辰大海</title><link ref="canonical" href="http://bujiuzhi.gitee.io/2022/02/13/3.%E5%B8%B8%E7%94%A8API/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.7.0"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement('script');
  hm.src = 'https://hm.baidu.com/hm.js?12b4347d0a1f32de969a2276a08cb466';
  hm.async = true;

  if (false) {
    hm.setAttribute('data-pjax', '');
  }
  var s = document.getElementsByTagName('script')[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":8},
  header: {"enable":true,"showOnPost":false,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"carbon","highlight":"light","wordWrap":false},
  reward: true,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner header-inner--height header-inner--bgcolor"><nav class="header-nav header-nav--sticky"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">3.常用API</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-13</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-09-14</span></span></div></header><div class="post-body">
        <h1 id="1-API"   >
          <a href="#1-API" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-API" class="headerlink" title="1.API"></a>1.API</h1>
      
        <h2 id="1-1-API概述"   >
          <a href="#1-1-API概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1-API概述" class="headerlink" title="1.1 API概述"></a>1.1 API概述</h2>
      <ul>
<li><p>什么是API</p>
<p>​	API (Application Programming Interface) ：应用程序编程接口</p>
</li>
<li><p>java中的API</p>
<p>​	指的就是 JDK 中提供的各种功能的 Java类，这些类将底层的实现封装了起来，我们不需要关心这些类是如何实现的，只需要学习这些类如何使用即可，我们可以通过帮助文档来学习这些API如何使用。</p>
</li>
</ul>

        <h2 id="1-2-键盘录入字符串"   >
          <a href="#1-2-键盘录入字符串" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-键盘录入字符串" class="headerlink" title="1.2 键盘录入字符串"></a>1.2 键盘录入字符串</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Scanner类 :</span><br><span class="line"></span><br><span class="line">		next() <span class="comment">//遇到了空格, 就不再录入数据了 , 结束标记: 空格, tab键</span></span><br><span class="line"></span><br><span class="line">		nextLine() <span class="comment">//可以将数据完整的接收过来 , 结束标记: 回车换行符       </span></span><br></pre></td></tr></table></div></figure>

<p><strong>代码实现 :</strong> </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.api;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Scanner</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        nextInt和nextLine方法配合使用的时候, nextLine方法就没有键盘录入的机会了</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        建议: 今后键盘录入数据的时候, 如果是字符串和整数一起接受, 建议使用next方法接受字符串.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入整数:&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> sc.nextInt(); <span class="comment">// 10 + 回车换行后，程序直接就结束了</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> sc.nextLine();</span><br><span class="line">      	<span class="comment">//String s = sc.next();//改成这个就没有问题了</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        System.out.println(num);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><img src="https://s2.loli.net/2022/02/13/IVlkUfGixAwSJ5D.png" alt="image-20220213181941454"></p>

        <h1 id="2-String类"   >
          <a href="#2-String类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-String类" class="headerlink" title="2.String类"></a>2.String类</h1>
      
        <h2 id="2-1-String概述"   >
          <a href="#2-1-String概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-String概述" class="headerlink" title="2.1 String概述"></a>2.1 String概述</h2>
      <p>​	1 String 类在 java.lang 包下，所以使用的时候不需要导包</p>
<p>​	2 String 类代表字符串，Java 程序中的所有字符串文字（例如“abc”）都被实现为此类的实例也就是说，Java 程序中所有的双引号字符串，都是 String 类的对象</p>
<p>​	3 字符串不可变，它们的值在创建后不能被更改</p>

        <h2 id="2-2-String类的构造方法"   >
          <a href="#2-2-String类的构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-String类的构造方法" class="headerlink" title="2.2 String类的构造方法"></a>2.2 String类的构造方法</h2>
      <p>常用的构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">()</span>	<span class="comment">//创建个空白字符串对象,不含有任何内容</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">(<span class="type">char</span>[] chs)</span>	<span class="comment">//根据字符数组的内容,来创建字符串对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">(String original)</span>	<span class="comment">//根据传入的字符串内容,来创建字符串对象</span></span><br><span class="line">  </span><br><span class="line"><span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>	<span class="comment">//直接赋值的方式创建字符串对象,内容就是abc</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.string;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2StringConstructor</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        String类常见构造方法:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            public String() : 创建一个空白字符串对象，不含有任何内容</span></span><br><span class="line"><span class="comment">            public String(char[] chs) : 根据字符数组的内容，来创建字符串对象</span></span><br><span class="line"><span class="comment">            public String(String original) : 根据传入的字符串内容，来创建字符串对象</span></span><br><span class="line"><span class="comment">            String s = “abc”;  直接赋值的方式创建字符串对象，内容就是abc</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">         注意:</span></span><br><span class="line"><span class="comment">                String这个类比较特殊, 打印其对象名的时候, 不会出现内存地址</span></span><br><span class="line"><span class="comment">                而是该对象所记录的真实内容.</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                面向对象-继承, Object类</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// public String() : 创建一个空白字符串对象，不含有任何内容</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>();</span><br><span class="line">        System.out.println(s1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// public String(char[] chs) : 根据字符数组的内容，来创建字符串对象</span></span><br><span class="line">        <span class="type">char</span>[] chs = &#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>&#125;;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(chs);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// public String(String original) : 根据传入的字符串内容，来创建字符串对象</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        System.out.println(s3);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-3-创建字符串对象的区别对比"   >
          <a href="#2-3-创建字符串对象的区别对比" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-创建字符串对象的区别对比" class="headerlink" title="2.3 创建字符串对象的区别对比"></a>2.3 创建字符串对象的区别对比</h2>
      <ul>
<li><p><strong>通过构造方法创建</strong></p>
<p>​	通过 new 创建的字符串对象，每一次 new 都会申请一个内存空间，虽然内容相同，但是地址值不同</p>
</li>
<li><p><strong>直接赋值方式创建</strong></p>
<p>​	以“”方式给出的字符串，只要字符序列相同(顺序和大小写)，无论在程序代码中出现几次，JVM 都只会建立一个 String 对象，并在字符串池中维护</p>
</li>
</ul>

        <h2 id="2-4-字符串的比较"   >
          <a href="#2-4-字符串的比较" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-字符串的比较" class="headerlink" title="2.4 字符串的比较"></a>2.4 字符串的比较</h2>
      
        <h3 id="2-4-1-字符串的比较"   >
          <a href="#2-4-1-字符串的比较" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-1-字符串的比较" class="headerlink" title="2.4.1 字符串的比较"></a>2.4.1 字符串的比较</h3>
      <ul>
<li>&#x3D;&#x3D; 比较基本数据类型：比较的是具体的值</li>
<li>&#x3D;&#x3D; 比较引用数据类型：比较的是对象地址值</li>
</ul>
<p>String类 :  <code>public boolean equals(String s)</code> <strong>比较两个字符串内容是否相同、区分大小写</strong></p>
<p><strong>代码 :</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.stringmethod;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Equals</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="string">&quot;ABC&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// equals : 比较字符串内容, 区分大小写</span></span><br><span class="line">        System.out.println(s1.equals(s2));<span class="comment">//false</span></span><br><span class="line">        System.out.println(s1.equals(s3));<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// equalsIgnoreCase : 比较字符串内容, 忽略大小写</span></span><br><span class="line">        System.out.println(s1.equalsIgnoreCase(s2));<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="2-5-String常用方法"   >
          <a href="#2-5-String常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-5-String常用方法" class="headerlink" title="2.5 String常用方法"></a>2.5 String常用方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object anObject)</span>  <span class="comment">//比较字符串的内容，严格区分大小写</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equalsIgnoreCase</span><span class="params">(String anotherString)</span>  <span class="comment">//比较字符串的内容，忽略大小写</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">length</span><span class="params">()</span>  <span class="comment">//返回此字符串的长度</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">char</span> <span class="title function_">charAt</span><span class="params">(<span class="type">int</span> index)</span>  <span class="comment">//返回指定索引处的 char 值</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">char</span>[] toCharArray()  <span class="comment">//将字符串拆分为字符数组后返回</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">substring</span><span class="params">(<span class="type">int</span> beginIndex, <span class="type">int</span> endIndex)</span>  <span class="comment">//根据开始和结束索引进行截取，得到新的字符串（包含头，不包含尾）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">substring</span><span class="params">(<span class="type">int</span> beginIndex)</span>  <span class="comment">//从传入的索引处截取，截取到末尾，得到新的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">replace</span><span class="params">(CharSequence target, CharSequence replacement)</span>  <span class="comment">//使用新值，将字符串中的旧值替换，得到新的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String[] split(String regex)  <span class="comment">//根据传入的规则切割字符串，得到字符串数组</span></span><br></pre></td></tr></table></div></figure>


        <h1 id="3-StringBuilder类"   >
          <a href="#3-StringBuilder类" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-StringBuilder类" class="headerlink" title="3.StringBuilder类"></a>3.StringBuilder类</h1>
      
        <h2 id="3-1-StringBuilder类概述"   >
          <a href="#3-1-StringBuilder类概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-StringBuilder类概述" class="headerlink" title="3.1 StringBuilder类概述"></a>3.1 StringBuilder类概述</h2>
      <p>​	StringBuilder 是一个可变的字符串类，我们可以把它看成是一个容器，这里的可变指的是 StringBuilder 对象中的内容是可变的</p>

        <h2 id="3-2-StringBuilder类和String类的区别"   >
          <a href="#3-2-StringBuilder类和String类的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-StringBuilder类和String类的区别" class="headerlink" title="3.2 StringBuilder类和String类的区别"></a>3.2 StringBuilder类和String类的区别</h2>
      <ul>
<li>String类：内容是<strong>不可变</strong>的</li>
<li>StringBuilder类：内容是<strong>可变</strong>的</li>
</ul>

        <h2 id="3-3-StringBuilder类的构造方法"   >
          <a href="#3-3-StringBuilder类的构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-3-StringBuilder类的构造方法" class="headerlink" title="3.3 StringBuilder类的构造方法"></a>3.3 StringBuilder类的构造方法</h2>
      <p>常用的构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">()</span>	<span class="comment">//创建一个空白可变字符串对象，不含有任何内容</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">(String str)</span>	<span class="comment">//根据字符串的内容，来创建可变字符串对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public StringBuilder()：创建一个空白可变字符串对象，不含有任何内容</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);		<span class="comment">//sb:</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb.length():&quot;</span> + sb.length());		<span class="comment">//sb.length():0</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder(String str)：根据字符串的内容，来创建可变字符串对象</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;sb2:&quot;</span> + sb2);		<span class="comment">//sb2:hello</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb2.length():&quot;</span> + sb2.length());		<span class="comment">//sb2.length():5</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-4-StringBuilder常用的成员方法"   >
          <a href="#3-4-StringBuilder常用的成员方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-4-StringBuilder常用的成员方法" class="headerlink" title="3.4 StringBuilder常用的成员方法"></a>3.4 StringBuilder常用的成员方法</h2>
      <ul>
<li>添加和反转方法</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> StringBuilder <span class="title function_">append</span><span class="params">(任意类型)</span>	<span class="comment">//添加数据，并返回对象本身</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> StringBuilder <span class="title function_">reverse</span><span class="params">()</span>	<span class="comment">//返回相反的字符序列</span></span><br></pre></td></tr></table></div></figure>

<ul>
<li>示例代码</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建对象</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder append(任意类型)：添加数据，并返回对象本身</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> sb.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb); <span class="comment">//sb:hello</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb2:&quot;</span> + sb2);<span class="comment">//sb2:hello</span></span><br><span class="line">        System.out.println(sb == sb2);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">        sb.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        sb.append(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        sb.append(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        sb.append(<span class="number">100</span>);</span><br><span class="line">        <span class="comment">//链式编程</span></span><br><span class="line">        sb.append(<span class="string">&quot;hello&quot;</span>).append(<span class="string">&quot;world&quot;</span>).append(<span class="string">&quot;java&quot;</span>).append(<span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);<span class="comment">//sb:hellohelloworldjava100helloworldjava100</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder reverse()：返回相反的字符序列</span></span><br><span class="line">        sb.reverse();</span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);<span class="comment">//sb:001avajdlrowolleh001avajdlrowolleholleh</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-5StringBuilder和String相互转换"   >
          <a href="#3-5StringBuilder和String相互转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-5StringBuilder和String相互转换" class="headerlink" title="3.5StringBuilder和String相互转换"></a>3.5StringBuilder和String相互转换</h2>
      <p>StringBuilder————》String:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span><span class="comment">//通过 toString() 就可以实现把 StringBuilder 转换为 String</span></span><br></pre></td></tr></table></div></figure>

<p>String————》StringBuilder:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">(String s)</span><span class="comment">//通过构造方法就可以实现把 String 转换为 StringBuilder</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//StringBuilder 转换为 String</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">        sb1.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        <span class="comment">//String s1 = sb; //这个是错误的做法</span></span><br><span class="line">        <span class="comment">//public String toString()：通过 toString() 就可以实现把 StringBuilder 转换为 String</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> sb1.toString();</span><br><span class="line">        System.out.println(s1);	<span class="comment">//hello</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//String 转换为 StringBuilder</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        <span class="comment">//StringBuilder sb = s; //这个是错误的做法</span></span><br><span class="line">        <span class="comment">//public StringBuilder(String s)：通过构造方法就可以实现把 String 转换为 StringBuilder</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(s2);</span><br><span class="line">        System.out.println(sb2);	<span class="comment">//hello</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="4-Math类"   >
          <a href="#4-Math类" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-Math类" class="headerlink" title="4.Math类"></a>4.Math类</h1>
      <p>1、Math类概述：包含执行基本数字运算的方法</p>
<p>2、Math中方法的调用方式：Math类中无构造方法，但内部的方法都是静态的，可以通过   <code>类名.方法名</code>进行调用</p>
<p>3、Math类的常用方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">abs</span><span class="params">(<span class="type">int</span> a)</span>	<span class="comment">//返回参数的绝对值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">ceil</span> <span class="params">(<span class="type">double</span> a)</span>	<span class="comment">//返回大于或等于参数的最小 double值,等于ー个整数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">floor</span><span class="params">(<span class="type">double</span> a)</span>	<span class="comment">//返回小于或等于参数的最大 double值,等于一个整数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">round</span><span class="params">(<span class="type">float</span> a)</span>	<span class="comment">//按照四舍五入返回最接近参数的int</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>	<span class="comment">//返回两个int值中的较大值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">min</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>	<span class="comment">//返回两个int值中的较小值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">pow</span><span class="params">( <span class="type">double</span> a, <span class="type">double</span> b)</span>	<span class="comment">//返回a的b次幂的值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">random</span><span class="params">()</span>	<span class="comment">//返回值为 double的正值,[0.0,1.0)</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="5-System类"   >
          <a href="#5-System类" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-System类" class="headerlink" title="5.System类"></a>5.System类</h1>
      <p>System类的常用方法 </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">exit</span><span class="params">(<span class="type">int</span> status)</span>		<span class="comment">//终止当前运行的Java虚拟机，非零表示异常终止</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">long</span> <span class="title function_">currentTimeMillis</span><span class="params">()</span>	<span class="comment">//返回当前时间(以毫秒为单位)</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">arraycopy</span><span class="params">(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)</span>	<span class="comment">//数组copy</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="6-Object类"   >
          <a href="#6-Object类" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-Object类" class="headerlink" title="6.Object类"></a>6.Object类</h1>
      <p>Object 是所有类的根，每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类，换句话说，该类所具备的方法，所有类都会有一份。</p>

        <h2 id="6-1-toString方法"   >
          <a href="#6-1-toString方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-toString方法" class="headerlink" title="6.1 toString方法"></a>6.1 toString方法</h2>
      <p>为了能以良好的格式，更方便的展示对象中的属性值，通常会在类中重写方法，把展示地址变为展示属性。</p>
<p>原始方法：</p>
<p><img src="https://s2.loli.net/2022/02/13/zPuypmwNJFTSLds.png" alt="toString方法的源码分析"></p>
<p>重写后：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">extends</span> <span class="title class_">Object</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s.setAge(<span class="number">30</span>);</span><br><span class="line">        System.out.println(s); </span><br><span class="line">        System.out.println(s.toString()); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>运行结果：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Student&#123;name=<span class="string">&#x27;林青霞&#x27;</span>, age=<span class="number">30</span>&#125;</span><br><span class="line">Student&#123;name=<span class="string">&#x27;林青霞&#x27;</span>, age=<span class="number">30</span>&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-2-equals方法"   >
          <a href="#6-2-equals方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-equals方法" class="headerlink" title="6.2 equals方法"></a>6.2 equals方法</h2>
      <p>重写equals方法的场景：不希望比较对象的地址值，想要结合对象属性进行比较的时候</p>
<p>原始方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object obj)</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> (<span class="built_in">this</span> == obj);<span class="comment">//比的是地址值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>示例代码：</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="comment">//this -- s1</span></span><br><span class="line">        <span class="comment">//o -- s2</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o; <span class="comment">//student -- s2</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s1.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s1.setAge(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s2.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s2.setAge(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求：比较两个对象的内容是否相同</span></span><br><span class="line">        System.out.println(s1.equals(s2));</span><br><span class="line">        <span class="comment">//重写了equals，就是true</span></span><br><span class="line">      	<span class="comment">//没重写，比的就是地址，返回false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>面试题</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InterviewTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">&quot;abc&quot;</span>);</span><br><span class="line">        <span class="comment">//此时调用的是String类中的equals方法。</span></span><br><span class="line">      	<span class="comment">//虽然String重写了equals方法，但方法中写了，若参数不是字符串，则直接fals；若参数是字符串才进行内容比较</span></span><br><span class="line">        System.out.println(s1.equals(sb)); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//StringBuilder类中是没有重写equals方法,用的就是Object类中的，比的就是地址值</span></span><br><span class="line">        System.out.println(sb.equals(s1)); <span class="comment">// false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="6-3-其他方法"   >
          <a href="#6-3-其他方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-其他方法" class="headerlink" title="6.3 其他方法"></a>6.3 其他方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(对象)</span>	<span class="comment">//返回参数中对象的字符串表示形式。</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(对象, 默认字符串)</span>	<span class="comment">//返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Boolean <span class="title function_">isNull</span><span class="params">(对象)</span>	<span class="comment">//判断对象是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Boolean <span class="title function_">nonNull</span><span class="params">(对象)</span>	<span class="comment">//判断对象是否不为空  </span></span><br></pre></td></tr></table></div></figure>




        <h1 id="7-BigDecimal类"   >
          <a href="#7-BigDecimal类" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-BigDecimal类" class="headerlink" title="7.BigDecimal类"></a>7.BigDecimal类</h1>
      <p>作用：可以用来进行精确计算</p>
<p>构造方法</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">BigDecimal(<span class="type">double</span> val)	<span class="comment">//参数为double</span></span><br><span class="line">  </span><br><span class="line">BigDecimal(String val)	<span class="comment">//参数为String</span></span><br></pre></td></tr></table></div></figure>

<p>常用方法</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">add</span><span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//加法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">subtract</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//减法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">multiply</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//乘法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">divide</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//除法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">divide</span> <span class="params">(另一个BigDecimal对象，精确几位，舍入模式)</span>	<span class="comment">//除法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_UP  进一法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_FLOOR 去尾法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_HALF_UP 四舍五入</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="8-包装类"   >
          <a href="#8-包装类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-包装类" class="headerlink" title="8.包装类"></a>8.包装类</h1>
      
        <h2 id="8-1-基本类型包装类"   >
          <a href="#8-1-基本类型包装类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-基本类型包装类" class="headerlink" title="8.1 基本类型包装类"></a>8.1 基本类型包装类</h2>
      <p>基本类型包装类的作用：将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据</p>
<p>常用的操作之一：用于基本数据类型与字符串之间的转换</p>
<p>基本类型对应的包装类：</p>
<div class="table-container"><table>
<thead>
<tr>
<th>基本数据类型</th>
<th>包装类</th>
</tr>
</thead>
<tbody><tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
<tr>
<td>char</td>
<td>Character</td>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
</tr>
</tbody></table></div>

        <h2 id="8-2-自动拆箱和自动装箱"   >
          <a href="#8-2-自动拆箱和自动装箱" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-自动拆箱和自动装箱" class="headerlink" title="8.2 自动拆箱和自动装箱"></a>8.2 自动拆箱和自动装箱</h2>
      <p>自动装箱：把基本数据类型转换为对应的包装类类型</p>
<p>自动拆箱：把包装类类型转换为对应的基本数据类型</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Integer</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">100</span>;  <span class="comment">// 自动装箱</span></span><br><span class="line">i += <span class="number">200</span>;         <span class="comment">// i = i + 200;  i + 200 自动拆箱；i = i + 200; 是自动装箱</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="8-3-Integer类"   >
          <a href="#8-3-Integer类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-Integer类" class="headerlink" title="8.3 Integer类"></a>8.3 Integer类</h2>
      <p>Integer类概述：包装一个对象中的原始类型 int 的值</p>
<p>Integer类构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">Integer</span><span class="params">(<span class="type">int</span> value)</span>	<span class="comment">//根据int值创建Integer对象(过时)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">Integer</span><span class="params">(String s)</span>	<span class="comment">//根据String值创建Integer对象(过时)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Integer <span class="title function_">valueOf</span><span class="params">(<span class="type">int</span> i)</span>	<span class="comment">//返回表示指定的int值的Integer实例</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Integer <span class="title function_">valueOf</span><span class="params">(String s)</span>	<span class="comment">//把String转为Integer类型</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public Integer(int value)：根据 int 值创建 Integer 对象(过时)</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Integer</span>(<span class="number">100</span>);</span><br><span class="line">        System.out.println(i1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public Integer(String s)：根据 String 值创建 Integer 对象(过时)</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Integer</span>(<span class="string">&quot;100&quot;</span>);</span><br><span class="line"><span class="comment">//        Integer i2 = new Integer(&quot;abc&quot;); //NumberFormatException</span></span><br><span class="line">        System.out.println(i2);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public static Integer valueOf(int i)：返回表示指定的 int 值的 Integer 实例</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i3</span> <span class="operator">=</span> Integer.valueOf(<span class="number">100</span>);</span><br><span class="line">        System.out.println(i3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public static Integer valueOf(String s)：返回一个保存指定值的Integer对象 String</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i4</span> <span class="operator">=</span> Integer.valueOf(<span class="string">&quot;100&quot;</span>);</span><br><span class="line">        System.out.println(i4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-4int和String类型的相互转换"   >
          <a href="#8-4int和String类型的相互转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4int和String类型的相互转换" class="headerlink" title="8.4int和String类型的相互转换"></a>8.4int和String类型的相互转换</h2>
      
        <h3 id="8-4-1-int转换为String"   >
          <a href="#8-4-1-int转换为String" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-1-int转换为String" class="headerlink" title="8.4.1 int转换为String"></a>8.4.1 int转换为String</h3>
      <p>转换方式:</p>
<ul>
<li>方式一：直接在数字后加一个空字符串</li>
<li>方式二：通过String类静态方法valueOf()</li>
</ul>
<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//int ---&gt; String</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式1</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> number + <span class="string">&quot;&quot;</span>;</span><br><span class="line">        System.out.println(s1);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//public static String valueOf(int i)</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> String.valueOf(number);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="8-4-2-String转换为int"   >
          <a href="#8-4-2-String转换为int" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-2-String转换为int" class="headerlink" title="8.4.2 String转换为int"></a>8.4.2 String转换为int</h3>
      <p>转换方式：</p>
<ul>
<li>方式一：先将字符串数字转成Integer，再调用valueOf()方法</li>
<li>方式二：通过Integer静态方法parseInt()进行转换</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//String ---&gt; int</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;100&quot;</span>;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式1：String --- Integer --- int</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i</span> <span class="operator">=</span> Integer.valueOf(s);</span><br><span class="line">        <span class="comment">//public int intValue()</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> i.intValue();</span><br><span class="line">        System.out.println(x);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//public static int parseInt(String s)</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> Integer.parseInt(s);</span><br><span class="line">        System.out.println(y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="9-Arrays类"   >
          <a href="#9-Arrays类" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-Arrays类" class="headerlink" title="9.Arrays类"></a>9.Arrays类</h1>
      <p>常用方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(<span class="type">int</span>[] a)</span>	<span class="comment">//返回指定数组的内容的字符串表示形式</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sort</span><span class="params">(<span class="type">int</span>[] a)</span>	<span class="comment">//按照数字顺序排列指定的数组</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">binarySearch</span><span class="params">(<span class="type">int</span>[] a, <span class="type">int</span> key)</span>	<span class="comment">//利用二分查找返回指定元素的索引</span></span><br></pre></td></tr></table></div></figure>






        <h1 id="10-时间日期类"   >
          <a href="#10-时间日期类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-时间日期类" class="headerlink" title="10.时间日期类"></a>10.时间日期类</h1>
      
        <h2 id="10-1-Date类"   >
          <a href="#10-1-Date类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-Date类" class="headerlink" title="10.1 Date类"></a>10.1 Date类</h2>
      <p>计算机中时间原点：1970年1月1日 00:00:00</p>
<p>时间换算单位：1秒 &#x3D; 1000毫秒</p>
<p>Date类概述：Date 代表了一个特定的时间，精确到毫秒</p>

        <h3 id="10-1-1-Date类构造方法"   >
          <a href="#10-1-1-Date类构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-1-Date类构造方法" class="headerlink" title="10.1.1 Date类构造方法"></a>10.1.1 Date类构造方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">Date</span><span class="params">()</span>	<span class="comment">//分配一个 Date对象，并初始化，以便它代表它被分配的时间，精确到毫秒</span></span><br><span class="line"><span class="keyword">public</span> <span class="title function_">Date</span><span class="params">(<span class="type">long</span> date)</span>	<span class="comment">//分配一个 Date对象，并将其初始化为表示从标准基准时间起指定的毫秒数</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DateDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public Date()：分配一个 Date对象，并初始化，以便它代表它被分配的时间，精确到毫秒</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        System.out.println(d1);</span><br><span class="line">      	<span class="comment">//Sun Feb 13 21:16:31 CST 2022	此时的时间</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public Date(long date)：分配一个 Date对象，并将其初始化为表示从标准基准时间起指定的毫秒数</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">date</span> <span class="operator">=</span> <span class="number">1000</span>*<span class="number">60</span>*<span class="number">60</span>;</span><br><span class="line">        <span class="type">Date</span> <span class="variable">d2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>(date);</span><br><span class="line">        System.out.println(d2);</span><br><span class="line">      	<span class="comment">//Thu Jan 01 09:00:00 CST 1970	从时间原点往后算data微秒的时刻</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-1-2-常用方法"   >
          <a href="#10-1-2-常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-2-常用方法" class="headerlink" title="10.1.2 常用方法"></a>10.1.2 常用方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">getTime</span><span class="params">()</span>	<span class="comment">//获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setTime</span><span class="params">(<span class="type">long</span> time)</span>	<span class="comment">//设置时间，给的是毫秒值</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DateDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建日期对象</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值</span></span><br><span class="line">        System.out.println(d.getTime());    <span class="comment">//1644758475285</span></span><br><span class="line">        System.out.println(d.getTime() * <span class="number">1.0</span> / <span class="number">1000</span> / <span class="number">60</span> / <span class="number">60</span> / <span class="number">24</span> / <span class="number">365</span> + <span class="string">&quot;年&quot;</span>);</span><br><span class="line">        <span class="comment">//52.15494911482116年</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public void setTime(long time):设置时间，给的是毫秒值</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">time</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        d.setTime(time);    <span class="comment">//Sun Feb 13 21:21:15 CST 2022</span></span><br><span class="line"></span><br><span class="line">        System.out.println(d);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h2 id="10-2-SimpleDateFormat类"   >
          <a href="#10-2-SimpleDateFormat类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-2-SimpleDateFormat类" class="headerlink" title="10.2 SimpleDateFormat类"></a>10.2 SimpleDateFormat类</h2>
      <p>概述：SimpleDateFormat是一个具体的类，用于以区域设置敏感的方式格式化和解析日期。</p>
<p>构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">SimpleDateFormat</span><span class="params">()</span>	<span class="comment">//构造一个SimpleDateFormat，使用默认模式和日期格式</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">SimpleDateFormat</span><span class="params">(String pattern)</span>	<span class="comment">//构造一个SimpleDateFormat使用给定的模式和默认的日期格式</span></span><br></pre></td></tr></table></div></figure>

<p>常用方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//格式化(从Date到String)</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String <span class="title function_">format</span><span class="params">(Date date)</span>	<span class="comment">//将日期格式化成日期/时间字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="comment">//解析(从String到Date)</span></span><br><span class="line">  <span class="keyword">public</span> Date <span class="title function_">parse</span><span class="params">(String source)</span>	<span class="comment">//从给定字符串的开始解析文本以生成日期</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SimpleDateFormatDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ParseException &#123;</span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//格式化：从 Date 到 String</span></span><br><span class="line"><span class="comment">//        SimpleDateFormat sdf = new SimpleDateFormat();//2022/2/13 下午9:28</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="comment">//2022年02月13日 21:28:53</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> sdf.format(d);</span><br><span class="line">        System.out.println(s);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//解析：从 String 到 Date</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">ss</span> <span class="operator">=</span> <span class="string">&quot;2048-08-09 11:11:11&quot;</span>;</span><br><span class="line">        <span class="comment">//ParseException</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">Date</span> <span class="variable">dd</span> <span class="operator">=</span> sdf2.parse(ss);</span><br><span class="line">        System.out.println(dd);<span class="comment">//Sun Aug 09 11:11:11 CST 2048</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="10-3-JDK8时间日期类"   >
          <a href="#10-3-JDK8时间日期类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-JDK8时间日期类" class="headerlink" title="10.3 JDK8时间日期类"></a>10.3 JDK8时间日期类</h2>
      <ul>
<li>LocalDate       表示日期（年月日）  </li>
<li>LocalTime       表示时间（时分秒）</li>
<li>LocalDateTime    表示时间+ 日期 （年月日时分秒）</li>
</ul>

        <h3 id="10-3-1-LocalDateTime创建方法"   >
          <a href="#10-3-1-LocalDateTime创建方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-1-LocalDateTime创建方法" class="headerlink" title="10.3.1 LocalDateTime创建方法"></a>10.3.1 LocalDateTime创建方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title function_">now</span><span class="params">()</span>	<span class="comment">//获取当前系统时间</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title function_">of</span><span class="params">(年,月,日,时,分,秒)</span>	<span class="comment">//使用指定年月日和时分秒初始化一个LocalDateTime对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">now</span> <span class="operator">=</span> LocalDateTime.now();</span><br><span class="line">        System.out.println(now);<span class="comment">//2022-02-13T22:36:17.005158</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>);</span><br><span class="line">        System.out.println(localDateTime);<span class="comment">//2020-11-11T11:11:11</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-2-LocalDateTime获取方法"   >
          <a href="#10-3-2-LocalDateTime获取方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-2-LocalDateTime获取方法" class="headerlink" title="10.3.2 LocalDateTime获取方法"></a>10.3.2 LocalDateTime获取方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getYear</span><span class="params">()</span>	<span class="comment">//获取年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMonthValue</span><span class="params">()</span>	<span class="comment">//获取月份(1-12)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDayofMonth</span><span class="params">()</span>	<span class="comment">//获取月份中的第几天(1-31)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDayofYeara</span><span class="params">()</span>	<span class="comment">//获取一年中的第几天(1-366)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> DayofWeek <span class="title function_">getDayofWeek</span><span class="params">()</span>	<span class="comment">//获取星期</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMinute</span><span class="params">()</span>	<span class="comment">//获取分钟</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getHour</span><span class="params">()</span>	<span class="comment">//获取小时</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="comment">//public int getYear()           获取年</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">year</span> <span class="operator">=</span> localDateTime.getYear();</span><br><span class="line">        System.out.println(<span class="string">&quot;年为&quot;</span> +year);<span class="comment">//年为2020</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getMonthValue()     获取月份（1-12）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">month</span> <span class="operator">=</span> localDateTime.getMonthValue();</span><br><span class="line">        System.out.println(<span class="string">&quot;月份为&quot;</span> + month);<span class="comment">//月份为11</span></span><br><span class="line"></span><br><span class="line">        <span class="type">Month</span> <span class="variable">month1</span> <span class="operator">=</span> localDateTime.getMonth();</span><br><span class="line">        System.out.println(month1);<span class="comment">//NOVEMBER</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getDayOfMonth()     获取月份中的第几天（1-31）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">day</span> <span class="operator">=</span> localDateTime.getDayOfMonth();</span><br><span class="line">        System.out.println(<span class="string">&quot;日期为&quot;</span> + day);<span class="comment">//日期为11</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getDayOfYear()      获取一年中的第几天（1-366）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">dayOfYear</span> <span class="operator">=</span> localDateTime.getDayOfYear();</span><br><span class="line">        System.out.println(<span class="string">&quot;这是一年中的第&quot;</span> + dayOfYear + <span class="string">&quot;天&quot;</span>);<span class="comment">//这是一年中的第316天</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public DayOfWeek getDayOfWeek()获取星期</span></span><br><span class="line">        <span class="type">DayOfWeek</span> <span class="variable">dayOfWeek</span> <span class="operator">=</span> localDateTime.getDayOfWeek();</span><br><span class="line">        System.out.println(<span class="string">&quot;星期为&quot;</span> + dayOfWeek);<span class="comment">//星期为WEDNESDAY</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getMinute()        获取分钟</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">minute</span> <span class="operator">=</span> localDateTime.getMinute();</span><br><span class="line">        System.out.println(<span class="string">&quot;分钟为&quot;</span> + minute);<span class="comment">//分钟为11</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//public int getHour()           获取小时</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">hour</span> <span class="operator">=</span> localDateTime.getHour();</span><br><span class="line">        System.out.println(<span class="string">&quot;小时为&quot;</span> + hour);<span class="comment">//小时为11</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-3-LocalDateTime转换方法"   >
          <a href="#10-3-3-LocalDateTime转换方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-3-LocalDateTime转换方法" class="headerlink" title="10.3.3 LocalDateTime转换方法"></a>10.3.3 LocalDateTime转换方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDate  <span class="title function_">toLocalDate</span> <span class="params">()</span>	<span class="comment">//转换成为一个LocalDate对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalTime <span class="title function_">toLocalTime</span> <span class="params">()</span>		<span class="comment">//转换成为一个LocalTime对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">12</span>, <span class="number">12</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">12</span>);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//public LocalDate toLocalDate ()    转换成为一个LocalDate对象</span></span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate</span> <span class="operator">=</span> localDateTime.toLocalDate();</span><br><span class="line">        System.out.println(localDate);<span class="comment">//2020-12-12</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public LocalTime toLocalTime ()    转换成为一个LocalTime对象</span></span><br><span class="line">        <span class="type">LocalTime</span> <span class="variable">localTime</span> <span class="operator">=</span> localDateTime.toLocalTime();</span><br><span class="line">        System.out.println(localTime);<span class="comment">//08:10:12</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-4-LocalDateTime格式化和解析"   >
          <a href="#10-3-4-LocalDateTime格式化和解析" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-4-LocalDateTime格式化和解析" class="headerlink" title="10.3.4 LocalDateTime格式化和解析"></a>10.3.4 LocalDateTime格式化和解析</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">format</span> <span class="params">(指定格式)</span>	<span class="comment">//把一个LocalDateTime格式化成为一个字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">parse</span> <span class="params">(准备解析的字符串, 解析格式)</span>	<span class="comment">//把一个日期字符串解析成为一个LocalDateTime对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> DateTimeFormatter <span class="title function_">ofPattern</span><span class="params">(String pattern)</span>	<span class="comment">//使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo5</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//method1();</span></span><br><span class="line">        <span class="comment">//method2();</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串</span></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>);</span><br><span class="line">        System.out.println(localDateTime);<span class="comment">//2020-11-12T13:14:15</span></span><br><span class="line">        <span class="type">DateTimeFormatter</span> <span class="variable">pattern</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> localDateTime.format(pattern);</span><br><span class="line">        System.out.println(s);<span class="comment">//2020年11月12日 13:14:15</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//public static LocalDateTime parse (准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;2020年11月12日 13:14:15&quot;</span>;</span><br><span class="line">        <span class="type">DateTimeFormatter</span> <span class="variable">pattern</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">parse</span> <span class="operator">=</span> LocalDateTime.parse(s, pattern);</span><br><span class="line">        System.out.println(parse);<span class="comment">//2020-11-12T13:14:15</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-5-LocalDateTime增加或者减少时间的方法"   >
          <a href="#10-3-5-LocalDateTime增加或者减少时间的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-5-LocalDateTime增加或者减少时间的方法" class="headerlink" title="10.3.5 LocalDateTime增加或者减少时间的方法"></a>10.3.5 LocalDateTime增加或者减少时间的方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusYears</span> <span class="params">(<span class="type">long</span> years)</span>	<span class="comment">//添加或者减去年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusMonths</span><span class="params">(<span class="type">long</span> months)</span>	<span class="comment">//添加或者减去月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusDays</span><span class="params">(<span class="type">long</span> days)</span>	<span class="comment">//添加或者减去日</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusHours</span><span class="params">(<span class="type">long</span> hours)</span>	<span class="comment">//添加或者减去时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusMinutes</span><span class="params">(<span class="type">long</span> minutes)</span>	<span class="comment">//添加或者减去分</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusSeconds</span><span class="params">(<span class="type">long</span> seconds)</span>	<span class="comment">//添加或者减去秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusWeeks</span><span class="params">(<span class="type">long</span> weeks)</span>	<span class="comment">//添加或者减去周</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-6-LocalDateTime减少或者增加时间的方法"   >
          <a href="#10-3-6-LocalDateTime减少或者增加时间的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-6-LocalDateTime减少或者增加时间的方法" class="headerlink" title="10.3.6 LocalDateTime减少或者增加时间的方法"></a>10.3.6 LocalDateTime减少或者增加时间的方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusYears</span> <span class="params">(<span class="type">long</span> years)</span>	<span class="comment">//减去或者添加年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusMonths</span><span class="params">(<span class="type">long</span> months)</span>	<span class="comment">//减去或者添加月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusDays</span><span class="params">(<span class="type">long</span> days)</span>	<span class="comment">//减去或者添加日</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusHours</span><span class="params">(<span class="type">long</span> hours)</span>	<span class="comment">//减去或者添加时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusMinutes</span><span class="params">(<span class="type">long</span> minutes)</span>	<span class="comment">//减去或者添加分</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusSeconds</span><span class="params">(<span class="type">long</span> seconds)</span>	<span class="comment">//减去或者添加秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusWeeks</span><span class="params">(<span class="type">long</span> weeks)</span>	<span class="comment">//减去或者添加周</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-7-LocalDateTime修改方法"   >
          <a href="#10-3-7-LocalDateTime修改方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-7-LocalDateTime修改方法" class="headerlink" title="10.3.7 LocalDateTime修改方法"></a>10.3.7 LocalDateTime修改方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withYear</span><span class="params">(<span class="type">int</span> year)</span>	<span class="comment">//直接修改年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withMonth</span><span class="params">(<span class="type">int</span> month)</span>	<span class="comment">//直接修改月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withDayOfMonth</span><span class="params">(<span class="type">int</span> dayofmonth)</span>	<span class="comment">//直接修改日期(一个月中的第几天)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withDayOfYear</span><span class="params">(<span class="type">int</span> dayOfYear)</span>	<span class="comment">//直接修改日期(一年中的第几天)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withHour</span><span class="params">(<span class="type">int</span> hour)</span>	<span class="comment">//直接修改小时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withMinute</span><span class="params">(<span class="type">int</span> minute)</span>	<span class="comment">//直接修改分钟</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withSecond</span><span class="params">(<span class="type">int</span> second)</span>	<span class="comment">//直接修改秒</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="10-4-Period类"   >
          <a href="#10-4-Period类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-4-Period类" class="headerlink" title="10.4 Period类"></a>10.4 Period类</h2>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Period <span class="title function_">between</span><span class="params">(开始时间,结束时间)</span>	<span class="comment">//计算两个“时间&quot;的间隔</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getYears</span><span class="params">()</span>	<span class="comment">//获得这段时间的年数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMonths</span><span class="params">()</span>	<span class="comment">//获得此期间的总月数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDays</span><span class="params">()</span>	<span class="comment">//获得此期间的天数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">toTotalMonths</span><span class="params">()</span>	<span class="comment">//获取此期间的总月数</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  计算两个时间的间隔</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo9</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public static Period between(开始时间,结束时间)  计算两个&quot;时间&quot;的间隔</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate1</span> <span class="operator">=</span> LocalDate.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate2</span> <span class="operator">=</span> LocalDate.of(<span class="number">2048</span>, <span class="number">12</span>, <span class="number">12</span>);</span><br><span class="line">        <span class="type">Period</span> <span class="variable">period</span> <span class="operator">=</span> Period.between(localDate1, localDate2);</span><br><span class="line">        System.out.println(period);<span class="comment">//P28Y11M11D</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getYears()         获得这段时间的年数</span></span><br><span class="line">        System.out.println(period.getYears());<span class="comment">//28</span></span><br><span class="line">        <span class="comment">//public int getMonths()        获得此期间的月数</span></span><br><span class="line">        System.out.println(period.getMonths());<span class="comment">//11</span></span><br><span class="line">        <span class="comment">//public int getDays()          获得此期间的天数</span></span><br><span class="line">        System.out.println(period.getDays());<span class="comment">//11</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public long toTotalMonths()   获取此期间的总月数</span></span><br><span class="line">        System.out.println(period.toTotalMonths());<span class="comment">//347</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h2 id="10-5-Duration类"   >
          <a href="#10-5-Duration类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-5-Duration类" class="headerlink" title="10.5 Duration类"></a>10.5 Duration类</h2>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="title function_">Durationbetween</span><span class="params">(开始时间,结束时间)</span>	<span class="comment">//计算两个“时间&quot;的间隔</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">toSeconds</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">toMillis</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的毫秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">toNanos</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的纳秒</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  计算两个时间的间隔</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo10</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public static Duration between(开始时间,结束时间)  计算两个“时间&quot;的间隔</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime1</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>);</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime2</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="type">Duration</span> <span class="variable">duration</span> <span class="operator">=</span> Duration.between(localDateTime1, localDateTime2);</span><br><span class="line">        System.out.println(duration);<span class="comment">//PT21H57M58S</span></span><br><span class="line">        <span class="comment">//public long toSeconds()	       获得此时间间隔的秒</span></span><br><span class="line">        System.out.println(duration.toSeconds());<span class="comment">//79078</span></span><br><span class="line">        <span class="comment">//public int toMillis()	           获得此时间间隔的毫秒</span></span><br><span class="line">        System.out.println(duration.toMillis());<span class="comment">//79078000</span></span><br><span class="line">        <span class="comment">//public int toNanos()             获得此时间间隔的纳秒</span></span><br><span class="line">        System.out.println(duration.toNanos());<span class="comment">//79078000000000</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ END ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">Author: </span><span class="copyright-author__value"><a href="http://bujiuzhi.gitee.io">不久</a></span></div><div class="copyright-link"><span class="copyright-link__name">Link: </span><span class="copyright-link__value"><a href="http://bujiuzhi.gitee.io/2022/02/13/3.%E5%B8%B8%E7%94%A8API/">http://bujiuzhi.gitee.io/2022/02/13/3.%E5%B8%B8%E7%94%A8API/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">Copyright: </span><span class="copyright-notice__value">All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> unless stating additionally</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="http://bujiuzhi.gitee.io/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="post-reward reward"><div class="reward-button">Buy me a coffee</div><div class="reward-qrcode"><span class="reward-qrcode-alipay"><img class="reward-qrcode-alipay__img" src="/images/alipay.jpg"><div class="reward-qrcode-alipay__text">Alipay</div></span><span class="reward-qrcode-wechat"><img class="reward-qrcode-wechat__img" src="/images/weixin.png"><div class="reward-qrcode-wechat__text">Wechat</div></span></div></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/2022/02/16/4.%E9%9B%86%E5%90%88/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">4.集合</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/2022/02/13/2.%E9%9D%A2%E5%AF%B9%E5%AF%B9%E8%B1%A1/"><span class="paginator-prev__text">2.面对对象</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">Catalog</span><span class="sidebar-nav-ov">Overview</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-API"><span class="toc-text">
          1.API</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-API%E6%A6%82%E8%BF%B0"><span class="toc-text">
          1.1 API概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E9%94%AE%E7%9B%98%E5%BD%95%E5%85%A5%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">
          1.2 键盘录入字符串</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-String%E7%B1%BB"><span class="toc-text">
          2.String类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-String%E6%A6%82%E8%BF%B0"><span class="toc-text">
          2.1 String概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-String%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="toc-text">
          2.2 String类的构造方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%88%9B%E5%BB%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%8C%BA%E5%88%AB%E5%AF%B9%E6%AF%94"><span class="toc-text">
          2.3 创建字符串对象的区别对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-text">
          2.4 字符串的比较</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-1-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-text">
          2.4.1 字符串的比较</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-String%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">
          2.5 String常用方法</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-StringBuilder%E7%B1%BB"><span class="toc-text">
          3.StringBuilder类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-StringBuilder%E7%B1%BB%E6%A6%82%E8%BF%B0"><span class="toc-text">
          3.1 StringBuilder类概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-StringBuilder%E7%B1%BB%E5%92%8CString%E7%B1%BB%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">
          3.2 StringBuilder类和String类的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-StringBuilder%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="toc-text">
          3.3 StringBuilder类的构造方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-StringBuilder%E5%B8%B8%E7%94%A8%E7%9A%84%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="toc-text">
          3.4 StringBuilder常用的成员方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5StringBuilder%E5%92%8CString%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-text">
          3.5StringBuilder和String相互转换</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Math%E7%B1%BB"><span class="toc-text">
          4.Math类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-System%E7%B1%BB"><span class="toc-text">
          5.System类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-Object%E7%B1%BB"><span class="toc-text">
          6.Object类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-toString%E6%96%B9%E6%B3%95"><span class="toc-text">
          6.1 toString方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-equals%E6%96%B9%E6%B3%95"><span class="toc-text">
          6.2 equals方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-%E5%85%B6%E4%BB%96%E6%96%B9%E6%B3%95"><span class="toc-text">
          6.3 其他方法</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-BigDecimal%E7%B1%BB"><span class="toc-text">
          7.BigDecimal类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E5%8C%85%E8%A3%85%E7%B1%BB"><span class="toc-text">
          8.包装类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#8-1-%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E5%8C%85%E8%A3%85%E7%B1%BB"><span class="toc-text">
          8.1 基本类型包装类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-2-%E8%87%AA%E5%8A%A8%E6%8B%86%E7%AE%B1%E5%92%8C%E8%87%AA%E5%8A%A8%E8%A3%85%E7%AE%B1"><span class="toc-text">
          8.2 自动拆箱和自动装箱</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-3-Integer%E7%B1%BB"><span class="toc-text">
          8.3 Integer类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-4int%E5%92%8CString%E7%B1%BB%E5%9E%8B%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-text">
          8.4int和String类型的相互转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-4-1-int%E8%BD%AC%E6%8D%A2%E4%B8%BAString"><span class="toc-text">
          8.4.1 int转换为String</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-4-2-String%E8%BD%AC%E6%8D%A2%E4%B8%BAint"><span class="toc-text">
          8.4.2 String转换为int</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-Arrays%E7%B1%BB"><span class="toc-text">
          9.Arrays类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E6%97%B6%E9%97%B4%E6%97%A5%E6%9C%9F%E7%B1%BB"><span class="toc-text">
          10.时间日期类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#10-1-Date%E7%B1%BB"><span class="toc-text">
          10.1 Date类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#10-1-1-Date%E7%B1%BB%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.1.1 Date类构造方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-1-2-%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.1.2 常用方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-2-SimpleDateFormat%E7%B1%BB"><span class="toc-text">
          10.2 SimpleDateFormat类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-3-JDK8%E6%97%B6%E9%97%B4%E6%97%A5%E6%9C%9F%E7%B1%BB"><span class="toc-text">
          10.3 JDK8时间日期类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-1-LocalDateTime%E5%88%9B%E5%BB%BA%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.1 LocalDateTime创建方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-2-LocalDateTime%E8%8E%B7%E5%8F%96%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.2 LocalDateTime获取方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-3-LocalDateTime%E8%BD%AC%E6%8D%A2%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.3 LocalDateTime转换方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-4-LocalDateTime%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%92%8C%E8%A7%A3%E6%9E%90"><span class="toc-text">
          10.3.4 LocalDateTime格式化和解析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-5-LocalDateTime%E5%A2%9E%E5%8A%A0%E6%88%96%E8%80%85%E5%87%8F%E5%B0%91%E6%97%B6%E9%97%B4%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.5 LocalDateTime增加或者减少时间的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-6-LocalDateTime%E5%87%8F%E5%B0%91%E6%88%96%E8%80%85%E5%A2%9E%E5%8A%A0%E6%97%B6%E9%97%B4%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.6 LocalDateTime减少或者增加时间的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3-7-LocalDateTime%E4%BF%AE%E6%94%B9%E6%96%B9%E6%B3%95"><span class="toc-text">
          10.3.7 LocalDateTime修改方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-4-Period%E7%B1%BB"><span class="toc-text">
          10.4 Period类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-5-Duration%E7%B1%BB"><span class="toc-text">
          10.5 Duration类</span></a></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/icons/cute.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">星夜兼程</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://gitee.com/bujiuzhi" target="_blank" rel="noopener" data-popover="social.gitee" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">gitee</span></a><a class="sidebar-ov-social-item" href="https://github.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a><a class="sidebar-ov-social-item" href="https://twitter.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Twitter" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-twitter"></i></span></a><a class="sidebar-ov-social-item" href="https://space.bilibili.com/340646348" target="_blank" rel="noopener" data-popover="Youtube" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">bilibili</span></a><a class="sidebar-ov-social-item" href="https://www.zhihu.com/people/bujiuzhi" target="_blank" rel="noopener" data-popover="Zhihu" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">知</span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">7</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">3</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">You have read </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>不久</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v6.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.7.0</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1.0.1/dist/canvas-nest.min.js" color="0,0,0" opacity="0.6" count="99" zIndex="-1"></script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick,mail,link';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: '5aO7FYnqH2acFvUnyGxifpjn-gzGzoHsz',
    appKey: 'eqj3y9AYWsueEDToYMjl0c5i',
    notify: true,
    verify: true,
    placeholder: 'Just go go',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/js/utils.js?v=2.7.0"></script><script src="/js/stun-boot.js?v=2.7.0"></script><script src="/js/scroll.js?v=2.7.0"></script><script src="/js/header.js?v=2.7.0"></script><script src="/js/sidebar.js?v=2.7.0"></script></body></html>